// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
trigger ONEN_HouseholdChangeInHouseholding on ONEN_Household__c (after update) {
	//system.debug('ONEN_HouseholdChangeInHouseholding FIRED');
	//list to hold HH's that are changing membership count
	List<Id> HHChanging = new List<Id>();
	//list to hold HH's that now have 0 members, for deleting
	List<ONEN_Household__c> HHForDelete = new List<ONEN_Household__c>();
	//list to hold Contacts that need to be updated with new Household Name and Greeting
	List<Contact> ContactsForUpdate = new List<Contact>();
	
	ONEN_Household__c HouseholdBeforeChange;
	
	//put all changing HH's in the map
	for (ONEN_Household__c HouseholdAfterChange : Trigger.new) {
		boolean householdAdded = false;
		HouseholdBeforeChange = trigger.oldmap.get(HouseholdAfterChange.id);
		boolean addressChange = HouseholdBeforeChange.MailingStreet__c!=HouseholdAfterChange.MailingStreet__c||HouseholdBeforeChange.MailingCity__c!=HouseholdAfterChange.MailingCity__c||HouseholdBeforeChange.MailingState__c!=HouseholdAfterChange.MailingState__c||HouseholdBeforeChange.MailingPostalCode__c!=HouseholdAfterChange.MailingPostalCode__c||HouseholdBeforeChange.MailingCountry__c!=HouseholdAfterChange.MailingCountry__c||HouseholdBeforeChange.MailingCounty__c!=HouseholdAfterChange.MailingCounty__c;
		boolean nameOverride = HouseholdBeforeChange.Override_Household_Name__c!=HouseholdAfterChange.Override_Household_Name__c||HouseholdBeforeChange.Override_Greeting__c!=HouseholdAfterChange.Override_Greeting__c;
		boolean memberCountChange = HouseholdBeforeChange.Member_Count__c!=HouseholdAfterChange.Member_Count__c;
		boolean emptyHousehold = HouseholdAfterChange.Member_Count__c==0;
		
		//if member count changes
		if ((memberCountChange&&!emptyHousehold)||addressChange||nameOverride) {
			//add to the changing list
			//HHChangingMap.put(HouseholdAfterChange.id,HouseholdAfterChange);
			HHChanging.add(HouseholdAfterChange.id);
			householdAdded=true;					
		} 
		
		if (emptyHousehold) {
			HHForDelete.add(HouseholdAfterChange);
		}

	}

	if(HHChanging.size()>0||HHForDelete.size()>0) { 
	//	system.debug('ONEN_HouseholdChangeInHouseholding EXECUTED');
		//map for all HH's that have different member counts
		//may be able to get rid of this SOQL
		Map<id,ONEN_Household__c> HHinTrigger = new Map<id,ONEN_Household__c>([Select Name, Greeting__c, Override_Household_Name__c, Override_Greeting__c,MailingStreet__c,MailingCity__c,MailingState__c,MailingPostalCode__c, MailingCountry__c, MailingCounty__c from ONEN_Household__c where id IN :HHChanging]);
		//Map<Id,ONEN_Household__c> HHinTrigger = HHChangingMap;
		//list of Contacts in those households
		if (HHinTrigger.size()>0) {
			ONEN_HouseholdMaintenance ThisHouseholdSet = new ONEN_HouseholdMaintenance(HHinTrigger); 
			
			ThisHouseholdSet.Rename();
		
		}
		if (HHForDelete.size()>0) {
		//	try {
				delete HHForDelete;
		//	} catch (System.DmlException e) {		
		//		for (Integer k = 0; k < e.getNumDml(); k++) {
		//		 System.debug(e.getDmlMessage(k)); 
		//		}
		//	}
		}
		
	} else {
		//system.debug('ONEN_HouseholdChangeInHouseholding DID NOT EXECUTE');
	}
}